package top;

/**
 * @author chenyw
 * @date 2022/7/11 20:38
 * @description 买卖股票，买卖次数不受限制；可以在同一天买进并且卖出
 */
public class Top122maxProfit {
/*    //动归
    public int maxProfit(int[] prices) {
        int n = prices.length;
        int[] dp = new int[n];
        dp[0] = 0;
        int min = prices[0];
        for (int i = 1; i < n; i++) {
            if (prices[i] <= min) {
                dp[i] = dp[i - 1];
                min = prices[i];
            } else {
                dp[i] = prices[i] - min + dp[i - 1];
                min = prices[i];
            }
        }
        return dp[n - 1];
    }*/

    //贪心：收集所有上坡
    public int maxProfit(int[] prices) {
        int n = prices.length;
        int res = 0;
        for (int i = 1; i < n; i++) {
            if (prices[i] > prices[i - 1]) {
                res += prices[i] - prices[i - 1];
            }
        }
        return res;
    }
}
